// pages/feedbackAll/feedbackAll.js
const app = getApp()
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    currentData: 0, //当前页面：0为待回应，1为已答复
    userType: "00",
    currentPageOne: 1, //待回应页面的页数，初始为1
    currentPageTwo: 1, //已答复页面的页数，初始为1
    currentModule: "", //若点击模块进入该页面，则会赋值为对应模块数字
    feedbacksToBeReplied: [],
    feedbacksReplied: [],
    requestParameters: "" //存放请求参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    _this.setData({
      userType: wx.getStorageSync('logindata').data.userType
    })
    if(_this.data.requestParameters == ""){
      _this.setData({
        requestParameters: options
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _this = this
    if(_this.data.requestParameters) {
      const options = _this.data.requestParameters // 读取缓存的请求参数
      if (options.module) { // 各模块页面
        _this.setData({
          currentModule: options.module
        })
        _this.loadModule("00", 1, _this.data.currentPageOne * 10)
        _this.loadModule("01", 1, _this.data.currentPageTwo * 10)
      } else if (options.isReplied == "f") { // 待回应页面
        _this.setData({
          currentData: 0
        })
        _this.loadPage("00", 1, _this.data.currentPageOne * 10)
        _this.loadPage("01", 1, _this.data.currentPageTwo * 10)
      } else if (options.isReplied == "t") { // 已答复页面
        _this.setData({
          currentData: 1
        })
        _this.loadPage("00", 1, _this.data.currentPageOne * 10)
        _this.loadPage("01", 1, _this.data.currentPageTwo * 10)
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 返回swiper的高度
   * @param {*} className 
   * @param {*} callback 
   */
  get_wxml(className, callback) {      
    wx.createSelectorQuery().selectAll(className).boundingClientRect().exec(callback)
  },

  /**
   * 点击切换，滑块index赋值
   */
  swichNav: function (e) {
    const that = this;
    const requestParameters = that.data.requestParameters
    if (that.data.currentData == e.target.dataset.current){
      return false;
    }else{
      that.setData({
        currentData: e.currentTarget.dataset.current
      })
    }

    if(e.currentTarget.dataset.current == "0") {
      requestParameters.isReplied = "f"
    } else if (e.currentTarget.dataset.current == "1") {
      requestParameters.isReplied = "t"
    }
    that.setData({
      requestParameters: requestParameters
    })
  },
  /**
   * 滑动到底部加载多10条数据
   */
  loadPageOne: function (e) {
    const _this = this
    if (app.feedbackData.feedbacksToBeRepliedNumber > _this.data.currentPageOne * 10) {
      _this.setData({
        currentPageOne: _this.data.currentPageOne + 1
      })
      if (!_this.data.currentModule){
        _this.loadPage("00", _this.data.currentPageOne, 10)
      } else {
        _this.loadModule("00", _this.data.currentPageOne, 10)
      }
    }
  },

  loadPageTwo: function (e) {
    const _this = this
    if (app.feedbackData.feedbacksRepliedNumber > _this.data.currentPageTwo * 10) {
      _this.setData({
        currentPageTwo: _this.data.currentPageTwo + 1
      })
      if (!_this.data.currentModule){
        _this.loadPage("01", _this.data.currentPageTwo, 10)
      } else {
        _this.loadModule("01", _this.data.currentPageTwo, 10)
      }
    }
  },

    /**
   * 获取当前页面的index
   */
  swichTab: function (e) {
    this.setData({
      currentData: e.detail.current
    })
  },
  changePageTab: function (e) {
    if(this.data.currentData == 0 && e.detail.source == "touch") {
      this.setData({
        currentData: 1
      })
    } else if(this.data.currentData == 1 && e.detail.source == "touch"){
      this.setData({
        currentData: 0
      })
    }
  },
   /**
   * 返回上一页
   */
  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 跳转到反馈详情页面
   */
  goToFeedbackDetail: function (e) {
    if(this.data.userType == "00"){
      wx.navigateTo({
        url: '/pages/feedbackDetail/feedbackDetail?feedbackId=' + e.currentTarget.dataset.feedbackid,
      })
    } else {
      wx.navigateTo({
        url: '/pages/feedbackDetailReply/feedbackDetailReply?feedbackId=' + e.currentTarget.dataset.feedbackid,
      })
    }
  },

  loadPage: function(replyStatus, currentPage, pageSize) {
    const _this = this
    wx.request({
      url: api.cloudGetAllQuestion,
      method: "POST",
      data: {
        replyStatus: replyStatus,
        userId: wx.getStorageSync('workNo'),
        pageSize: pageSize,
        currentPage: currentPage,
        userType: _this.data.userType
      },
      header: {
      'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.data) {
          if(replyStatus == "00"){
            if(pageSize == 10 && currentPage != 1) {
              _this.setData({
                feedbacksToBeReplied: _this.data.feedbacksToBeReplied.concat(res.data.data.records)
              })
            } else {
              _this.setData({
                feedbacksToBeReplied: res.data.data.records
              })
            }
          } else if (replyStatus == "01") {
            if(pageSize == 10 && currentPage != 1) {
              _this.setData({
                feedbacksReplied: _this.data.feedbacksReplied.concat(res.data.data.records)
              })
            } else {
              _this.setData({
                feedbacksReplied: res.data.data.records
              })
            }
          }
        }
      }
    })
  },
  loadModule: function(replyStatus, currentPage, pageSize) {
    const _this = this
    wx.request({
      url: api.cloudGetAllModuleQuestion,
      method: "POST",
      data: {
        userId: wx.getStorageSync('workNo'),
        moduleType: _this.data.currentModule,
        replyStatus: replyStatus,
        pageSize: pageSize,
        currentPage: currentPage,
        userType: _this.data.userType
      },
      header: {
      'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.data) {
          if(replyStatus == "00"){
            if(pageSize == 10 && currentPage != 1) {
              _this.setData({
                feedbacksToBeReplied: _this.data.feedbacksToBeReplied.concat(res.data.data.records)
              })
            } else {
              _this.setData({
                feedbacksToBeReplied: res.data.data.records
              })
            }
          } else if (replyStatus == "01") {
            if(pageSize == 10 && currentPage != 1) {
              _this.setData({
                feedbacksReplied: _this.data.feedbacksReplied.concat(res.data.data.records)
              })
            } else {
              _this.setData({
                feedbacksReplied: res.data.data.records
              })
            }
          }
        } else if(res.data.msg == "查询结果为空") {
          if(replyStatus == "00"){
            _this.setData({
              feedbacksToBeReplied: []
            })
          } else if (replyStatus == "01") {
            _this.setData({
              feedbacksReplied: []
            })
          }
        }
      }
    })
  },
  tapToAddFeedback: function(){
    wx.navigateTo({
      url: '/pages/feedbackAdd/feedbackAdd'
    })
  }

})